﻿using System;
using System.Linq;
using System.Web.Mvc;
using Mvcmediaplayer.Models;
using Mvcmediaplayer.ViewModels;
using Mvcmediaplayer.Validation;

namespace Mvcmediaplayer.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        mediaEntities mediaDB = new mediaEntities();


        public ActionResult Index( )
        {

            // Retrieve Video from database
            var VideoModel = mediaDB.Videos.Include("Comments").Include("Ratings").FirstOrDefault();
   
            var viewModel = new VideoBrowseViewModel()
            {
                Video = VideoModel
            };

            return View(viewModel);

                  }

        public ActionResult About()
        {
            return View();
        }

        [HttpPost]
        public virtual JsonResult SaveRating(int videoId, float rating)
        {
            try
            {
                Rating videoRating = new Rating();
                videoRating.VideoId = videoId;
                videoRating.Rating1 = rating;
                videoRating.Datetime = System.DateTime.Now;
                videoRating.User = "Nicholas";

                mediaDB.AddToRatings(videoRating);
                mediaDB.SaveChanges();

                var ratings = mediaDB.Ratings.Where(x => x.VideoId == videoId);

                return Json(new JsonResponse
                    {
                        Success = true,
                        Message = "Thanks for your rating",
                        Result = new
                        {
                            AverageScore = ratings.Average(x => x.Rating1).ToString().Substring(0,4),
                            Votes = ratings.Count()
                        }
                    });
            }
            catch (Exception ex)
            {
                return Json(new JsonResponse
                    {
                        Success = false,
                        Message = ex.Message
                    });
            }
        }

        [HttpPost]
        public virtual JsonResult SavingComment(int videoId, string User, string Subject, string Content)
        { 
            VComment comment = new VComment() { VideoId = videoId, VUser = User, VSubject = Subject, VContent = Content };
            //validation information
          //  foreach (var err in comment.GetErrors())
     //           Error = Error + "{" + "\"" + "PropertyName" + "\"" + ":" + "\"" + err.PropertyName + "\"" + "," + "\"" + "Error" + ":" + "\"" + err.ErrorMessage + "}" + ",";
           

            if (comment.GetErrors().Count()==0)
            {
                mediaDB.AddToComments(new Comment() {  VideoId=videoId,User=User,Subject=Subject,Content=Content,Datetime=System.DateTime.Now});
                mediaDB.SaveChanges();
                return Json(new JsonResponse
                {
                    Success = true,
                    Message = System.DateTime.Now.ToString()
                });
            }
            else
            {
                // trim this string and replace the last 
              //  Error ="{"+ Error.Substring(0, Error.LastIndexOf(","))+"}";

                return Json(new JsonResponse
                    {
                        Success=false,
                        Message="not  validated",
                        Result=  comment.GetErrors().ToList()
                    });
            }

        }
    }
}
